package mazeviejuno;

import java.util.Random;

public class Cell {
	public boolean isUsed = false;
	private int xpos=0;
	private int ypos=0;
	public boolean nGate = false;
	public boolean sGate = false;
	public boolean eGate = false;
	public boolean wGate = false;
	

	
	public Cell(int i, int j) {
		this.setPos(i, j);
	}

	public Cell goToNextCell(Map map) {
		Random a = new Random();
		int rnd = a.nextInt(4);
		this.isUsed = true;
		
		switch(rnd) {
			case 0: {
				if (this.ypos == 0)
					return goToNextCell(map);
				else {
					map.breakBond(this,rnd);
					if (map.getCell(this.xpos,this.ypos-1).isUsed)
						return null;
					return map.getCell(this.xpos,this.ypos-1);
				}
			}
			case 1: {
				if (this.xpos == map.getWidth()-1)
					return goToNextCell(map);
				else {
					map.breakBond(this,rnd);
					if (map.getCell(this.xpos+1,this.ypos).isUsed)
						return null;
					return map.getCell(this.xpos+1,this.ypos);
				}
			}
			case 2: {
				if (this.ypos == map.getHeight()-1)
					return goToNextCell(map);
				else {
					map.breakBond(this,rnd);
					if (map.getCell(this.xpos,this.ypos+1).isUsed)
						return null;
					return map.getCell(this.xpos,this.ypos+1);
				}
			}
			case 3: {
				if (this.xpos == 0)
					return goToNextCell(map);
				else {
					map.breakBond(this,rnd);
					if (map.getCell(this.xpos-1,this.ypos).isUsed)
						return null;
					return map.getCell(this.xpos-1,this.ypos);
				}
			}
		}
		System.out.println("Hola");
		return null;
	}
	
	public int getXpos() {
		return xpos;
	}
	public void setXpos(int xpos) {
		this.xpos = xpos;
	}
	public int getYpos() {
		return ypos;
	}
	public void setYpos(int ypos) {
		this.ypos = ypos;
	}
	public void setPos(int xpos, int ypos) {
		this.setXpos(xpos);
		this.setYpos(ypos);
	}

	public void open(int rnd) {
		switch(rnd) {
		case 0: this.nGate = true; break;
		case 1: this.eGate = true; break;
		case 2: this.sGate = true; break;
		case 3: this.wGate = true; break;
		}
	}

}
